using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class BangCanDoiKeToanDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SaveBangCanDoiKeToan(BangCanDoiKeToan bangcandoiketoan)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddBangCanDoiKeToan", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddBangCanDoiKeToan";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, bangcandoiketoan.Id);
			cmd.Parameters.Add(param);
			param = CreateParameter("@TenChiTieu", DbType.String, ParameterDirection.Input, bangcandoiketoan.TenChiTieu);
			cmd.Parameters.Add(param);
			param = CreateParameter("@MaSo", DbType.String, ParameterDirection.Input, bangcandoiketoan.MaSo);
			cmd.Parameters.Add(param);
			param = CreateParameter("@ThuyetMinh", DbType.String, ParameterDirection.Input, bangcandoiketoan.ThuyetMinh);
			cmd.Parameters.Add(param);
			param = CreateParameter("@TKNo", DbType.String, ParameterDirection.Input, bangcandoiketoan.TKNo);
			cmd.Parameters.Add(param);
			param = CreateParameter("@TKCo", DbType.String, ParameterDirection.Input, bangcandoiketoan.TKCo);
			cmd.Parameters.Add(param);
            param = CreateParameter("@SoDauKy", DbType.Int64, ParameterDirection.Input, bangcandoiketoan.SoDauKy);
            cmd.Parameters.Add(param);
            param = CreateParameter("@SoCuoiKy", DbType.Int64, ParameterDirection.Input, bangcandoiketoan.SoCuoiKy);
            cmd.Parameters.Add(param);
			param = CreateParameter("@IsMainTitle", DbType.Int32, ParameterDirection.Input, bangcandoiketoan.IsMainTitle);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IsFirstSubTitle", DbType.Int32, ParameterDirection.Input, bangcandoiketoan.IsFirstSubTitle);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IsSecondSubTitle", DbType.Int32, ParameterDirection.Input, bangcandoiketoan.IsSecondSubTitle);
			cmd.Parameters.Add(param);
            param = CreateParameter("@IsThirdSubTitle", DbType.Int32, ParameterDirection.Input, bangcandoiketoan.IsThirdSubTitle);
            cmd.Parameters.Add(param);
			param = CreateParameter("@IsNegative", DbType.Int32, ParameterDirection.Input, bangcandoiketoan.IsNegative);
			cmd.Parameters.Add(param);
            param = CreateParameter("@Parent", DbType.Int32, ParameterDirection.Input, bangcandoiketoan.Parent);
            cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}
		
		// Get all BangCanDoiKeToan in DB
		public ResultObj GetAllBangCanDoiKeToan( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<BangCanDoiKeToan> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllBangCanDoiKeToan", CommandType.StoredProcedure);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<BangCanDoiKeToan>();
			BangCanDoiKeToan item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new BangCanDoiKeToan();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.TenChiTieu = Convert.ToString(reader["TenChiTieu"]);
			item.MaSo = Convert.ToString(reader["MaSo"]);
			item.ThuyetMinh = Convert.ToString(reader["ThuyetMinh"]);
			item.TKNo = Convert.ToString(reader["TKNo"]);
			item.TKCo = Convert.ToString(reader["TKCo"]);
            item.SoDauKy = Convert.ToInt64(reader["SoDauKy"]);
            item.SoCuoiKy = Convert.ToInt64(reader["SoCuoiKy"]);
			item.IsMainTitle = Convert.ToInt32(reader["IsMainTitle"]);
			item.IsFirstSubTitle = Convert.ToInt32(reader["IsFirstSubTitle"]);
			item.IsSecondSubTitle = Convert.ToInt32(reader["IsSecondSubTitle"]);
            item.IsThirdSubTitle = Convert.ToInt32(reader["IsThirdSubTitle"]);
			item.IsNegative = Convert.ToInt32(reader["IsNegative"]);
            item.Parent = Convert.ToInt32(reader["Parent"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all BangCanDoiKeToan in DB with condition
		public ResultObj GetAllBangCanDoiKeToanByID(BangCanDoiKeToan bangcandoiketoan)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<BangCanDoiKeToan> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllBangCanDoiKeToanByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, bangcandoiketoan.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<BangCanDoiKeToan>();
			BangCanDoiKeToan item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new BangCanDoiKeToan();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.TenChiTieu = Convert.ToString(reader["TenChiTieu"]);
			item.MaSo = Convert.ToString(reader["MaSo"]);
			item.ThuyetMinh = Convert.ToString(reader["ThuyetMinh"]);
			item.TKNo = Convert.ToString(reader["TKNo"]);
			item.TKCo = Convert.ToString(reader["TKCo"]);
            item.SoDauKy = Convert.ToInt64(reader["SoDauKy"]);
            item.SoCuoiKy = Convert.ToInt64(reader["SoCuoiKy"]);
			item.IsMainTitle = Convert.ToInt32(reader["IsMainTitle"]);
			item.IsFirstSubTitle = Convert.ToInt32(reader["IsFirstSubTitle"]);
			item.IsSecondSubTitle = Convert.ToInt32(reader["IsSecondSubTitle"]);
            item.IsThirdSubTitle = Convert.ToInt32(reader["IsThirdSubTitle"]);
			item.IsNegative = Convert.ToInt32(reader["IsNegative"]);
            item.Parent = Convert.ToInt32(reader["Parent"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a BangCanDoiKeToan out of DB with condition
		public ResultObj DeleteBangCanDoiKeToan(BangCanDoiKeToan bangcandoiketoan)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeleteBangCanDoiKeToan", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeleteBangCanDoiKeToan";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, bangcandoiketoan.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
					
	}
}
